package com.F09.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.F09.actionform.F0902ActionForm;
import com.F09.dao.RoleDAO;
import com.action.BaseAction;
import com.config.Constants;
import com.info.RoleInfo;
import com.util.BuildMessages;
import com.util.CommonTools;
/**
 * ��ɫ�����½����༭����Ȩ��ɾ�����
 * @file F0902Action.java
 * @author Liushimin
 * @since 2008-10-14
 * @version 1.0
 */
public class F0902Action extends BaseAction {
	

	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		F0902ActionForm actionForm = (F0902ActionForm) form;
	    RoleDAO dao =new RoleDAO();
		RoleInfo info = new RoleInfo();
		String operationType = actionForm.getOperationType();
		String strDetail = "detail";
		String strList = "list";
		String strRole = "role";
		String strSubmit = "submit";
		String strSubmitAdd = "submitAdd";
		String strAddLeft = "addLeft";
		String strAddRight = "addRight";
		String strAddFunction = "addFunction";
		if(CommonTools.isNullString(actionForm.getSearchState())){
			actionForm.setSearchState(Constants.STATE_01);
		}
		try {
			if(strSubmit.equals(operationType)){		
				info.setROLEID(actionForm.getROLEID());
				info.setCODE(actionForm.getCODE());
				info.setNAME(actionForm.getNAME());				
				info.setNOTES(actionForm.getNOTES());
		
				List list = new ArrayList();
				list = dao.selectByCode(info);
				if(list.isEmpty()){ //list == null || list.size() == 0   
					try{
						if("".equals(actionForm.getROLEID())){	
							dao.insert(info);
						}
						if(!("".equals(actionForm.getROLEID()))){	
							dao.update(info);
						}
						operationType=Constants.QUERYLIST;
						actionForm.setErrorFlag("保存成功！");
					}catch(Exception e){
						actionForm.setErrorFlag("����ʧ��保存失败！");
						e.printStackTrace();
					}
					}else {	
						super.saveMessages(request, BuildMessages.build("common.message"));
						return mapping.findForward(strDetail);
					}
			}
								
			if(strSubmitAdd.equals(operationType)){
				if(strAddRight.equals(actionForm.getType())){
					try{
						dao.insertRoleFun(CommonTools.array2String((String [])actionForm.getArrayID()),actionForm.getROLEID());
					    //actionForm.setErrorFlag("����ɹ�!!!");
					}catch(Exception e){
						//actionForm.setErrorFlag("����ʧ��!!!");
						e.printStackTrace();
					}
				}
				if(strAddLeft.equals(actionForm.getType())){
					try{
						dao.deleteByRoleFun(CommonTools.array2String((String [])actionForm.getArrayID()),actionForm.getROLEID());
					    //actionForm.setErrorFlag("ɾ��ɹ�!!!");
					}catch(Exception e){
						//actionForm.setErrorFlag("ɾ��ʧ��!!!");
						e.printStackTrace();
					}	
			    }
				operationType=strRole;
			}

			if(Constants.DELETE.equals(operationType)){ //F0902.jsp���ɾ�����
				try{
					dao.deleteByKey(CommonTools.null2String(actionForm.getROLEID()));
					operationType=Constants.QUERYLIST;
					actionForm.setErrorFlag("删除成功！");
				}catch(Exception e){
					actionForm.setErrorFlag("删除失败！");
					e.printStackTrace();
				}
			}
			
			if(Constants.ADD.equals(operationType)){ //F0902.jsp������������ת
				actionForm.setROLEID("");
				actionForm.setCODE("");
				actionForm.setNAME("");
				actionForm.setNOTES("");
				return mapping.findForward(strDetail);	
			}
			if(Constants.EDIT.equals(operationType)){ //F0902.jsp���༭������ת
				Map map = null;
				List list = new ArrayList();
				list = dao.selectById(actionForm.getROLEID());
				 for(int i = 0; i < list.size();i++){
					map = (Map)list.get(i);	
					actionForm.setCODE(CommonTools.null2String((String)map.get("CODE")));
					actionForm.setNAME(CommonTools.null2String((String)map.get("NAME")));
					actionForm.setNOTES(CommonTools.null2String((String)map.get("NOTES")));
					actionForm.setROLEID(CommonTools.null2String((String)map.get("ROLEID")));
				}
				return mapping.findForward(strDetail);	
			}
			
			if(Constants.BACK.equals(operationType)){ //F0902Detail.jsp���ػص�F0902.jspҳ��
				return mapping.findForward(strList);
			}
			
			if(strRole.equals(operationType)){ //F0902.jsp�����������ת(�����ܵ�����õĽ�ɫ)
				actionForm.setArrayID(null);
				actionForm.setLeftList(dao.selectByFunLift(actionForm.getROLEID()));
				actionForm.setRightList(dao.selectByFunRight(actionForm.getROLEID()));
				
				return mapping.findForward(strAddFunction);	
			}
			if(Constants.QUERYLIST.equals(operationType)){ //F0902.jsp����ҳ���ʼ����ѯ				
				actionForm.setList(dao.selectByObject());				
				return mapping.findForward(strList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dao != null) dao.disConnect();
		}		
		return mapping.findForward(strList);
	}
}
